const ledgerConfig = function () {
  return {
    default: { id: 0, type: 0, date: '', money: '', desc: '', category: 0 },
    type: ["支出", "收入", "不计收支"],
    category: [
      { name: "餐饮", icon: "ic_canyin.png", iconSelect: "ic_canyin_fs.png", type: 0 },
      { name: "服饰", icon: "ic_fushi.png", iconSelect: "ic_fushi_fs.png", type: 0 },
      { name: "购物", icon: "ic_gouwu.png", iconSelect: "ic_gouwu_fs.png", type: 0 },
      { name: "交通", icon: "ic_jiaotong.png", iconSelect: "ic_jiaotong_fs.png", type: 0 },
      { name: "零食", icon: "ic_lingshi.png", iconSelect: "ic_lingshi_fs.png", type: 0 },
      { name: "人情往来", icon: "ic_renqingwanglai.png", iconSelect: "ic_renqingwanglai_fs.png", type: 0 },
      { name: "日用品", icon: "ic_riyongpin.png", iconSelect: "ic_riyongpin_fs.png", type: 0 },
      { name: "食材", icon: "ic_shicai.png", iconSelect: "ic_shicai_fs.png", type: 0 },
      { name: "水电费", icon: "ic_shuidianfei.png", iconSelect: "ic_shuidianfei_fs.png", type: 0 },
      { name: "通讯", icon: "ic_tongxun.png", iconSelect: "ic_tongxun_fs.png", type: 0 },
      { name: "学习", icon: "ic_xuexi.png", iconSelect: "ic_xuexi_fs.png", type: 0 },
      { name: "烟酒", icon: "ic_yanjiu.png", iconSelect: "ic_yanjiu_fs.png", type: 0 },
      { name: "医疗", icon: "ic_yiliao.png", iconSelect: "ic_yiliao_fs.png", type: 0 },
      { name: "娱乐", icon: "ic_yule.png", iconSelect: "ic_yule_fs.png", type: 0 },
      { name: "住房", icon: "ic_zhufang.png", iconSelect: "ic_zhufang_fs.png", type: 0 },
      { name: "其它支出", icon: "ic_qita.png", iconSelect: "ic_qita_fs.png", type: 0 },

      { name: "薪资", icon: "in_xinzi.png", iconSelect: "in_xinzi_fs.png", type: 1 },
      { name: "二手交易", icon: "in_ershoushebei.png", iconSelect: "in_ershoushebei_fs.png", type: 1 },
      { name: "奖金", icon: "in_jiangjin.png", iconSelect: "in_jiangjin_fs.png", type: 1 },
      { name: "借入", icon: "in_jieru.png", iconSelect: "in_jieru_fs.png", type: 1 },
      { name: "收债", icon: "in_shouzhai.png", iconSelect: "in_shouzhai_fs.png", type: 1 },
      { name: "利息收入", icon: "in_lixifuji.png", iconSelect: "in_lixifuji_fs.png", type: 1 },
      { name: "投资汇报", icon: "in_touzi.png", iconSelect: "in_touzi_fs.png", type: 1 },
      { name: "意外", icon: "in_yiwai.png", iconSelect: "in_yiwai_fs.png", type: 1 },
      { name: "其它收入", icon: "in_qt.png", iconSelect: "in_qt_fs.png", type: 1 }],

  }
}
const ledgerTools = function () {
  return {
    addLedger(ledgerObj) {
      var data = wx.getStorageSync('ledgerData') || []
      ledgerObj.id = Date.now() + '' + data.length
      data.unshift(ledgerObj)
      data = data.sort((a, b) => Date.parse(b.date) - Date.parse(a.date))
      try {
        wx.setStorageSync('ledgerData', data)
      } catch (error) {
        wx.showToast({ icon: 'error', title: '添加出错\n' + error, })
      }
    },
    removeLedger(ledgetId) {
      const data = wx.getStorageSync('ledgerData') || []
      if (data.length == 0) { return }
      const index = data.findIndex(item => item.id == ledgetId)
      data.splice(index, 1)
      try {
        wx.setStorageSync('ledgerData', data)
        wx.showToast({ title: '已删除！', icon: "success" })
      } catch (error) {
        wx.showToast({ icon: 'error', title: '删除出错\n' + error, })
      }
    },
    updateLedger(ledgerObj) {
      const data = wx.getStorageSync('ledgerData') || []
      if (data.length == 0) { return }
      const index = data.findIndex(item => item.id == ledgetId)
      if (index < 0) {
        wx.showToast({ icon: 'error', title: '没有找到该条目\n' + ledgerObj.id })
        return
      }
      data[index].type = ledgerObj.type
      data[index].date = ledgerObj.date
      data[index].money = ledgerObj.money
      data[index].desc = ledgerObj.desc
      data[index].category = ledgerObj.category
      try {
        wx.setStorageSync('ledgerData', data)
      } catch (error) {
        wx.showToast({ icon: 'error', title: '更改出错\n' + error, })
      }
    },
    getData() {
      return wx.getStorageSync('ledgerData') || []
    },
    getDataById(ledgetId){
      const data = wx.getStorageSync('ledgerData') || []
      if (!ledgetId || ledgetId.length == 0) { return }
      const index = data.findIndex(item => item.id == ledgetId)
      return data[index] 
    },
    getDataByDate(yearAndMonth) {
      const data = wx.getStorageSync('ledgerData') || []
      if (data.length == 0) { return }
      return data.filter(item => item.date.startsWith(yearAndMonth)) || []
    },
    getDataDateGroupByDay(yearAndMonth) {
      const data = this.getDataByDate(yearAndMonth)
      if (data.length == 0) { return }
      const result = {}
      for (let item of data) {
        if (result[item.date]) {
          result[item.date].push(item)
        } else {
          result[item.date] = [item]
        }
      }
      return result;
    }
  }
}()

module.exports = { ledgerConfig, ledgerTools }